<?php

namespace App\Modules\LuckDraw\Exp;

use App\Modules\LuckDraw\Lib\StrLib;
use App\Modules\LuckDraw\Models\ExpModel;

class ExpFactory {
    private static $expInstances;
    //获取一个奖品实例
    public static function get(int $expId) : ?ExpInterface {
        if ($expId <= 0) {
            return null;
        }

        if (isset(self::$expInstances[$expId])) {
            return self::$expInstances[$expId];
        }

        $expModel = ExpModel::query()->find($expId);
        if ($expModel == null) {
            return null;
        }
        $className = __NAMESPACE__ . '\\' . StrLib::camelize($expModel->en_name) . 'Exp';
        if(!class_exists($className)){
            return null;
        }

        $expInstance =  new $className($expModel->id, $expModel->name, $expModel->en_name);
        self::$expInstances[$expId] = $expInstance;
        return $expInstance;
    }
}